package com.bst.etl.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.bst.base.domain.BaseDb;
import com.bst.base.service.IBaseDbService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.enums.DataSourceTypeEnum;
import com.bst.common.utils.StringUtils;
import com.bst.etl.factory.dbtype.ITypeMappingStrategy;
import com.bst.etl.utils.DDLUtil;
import com.bst.etl.vo.TableColumnsParamVO;
import com.bst.etl.vo.TableCreatParamVO;
import com.bst.etl.vo.TableForHosAndOdsParamVO;
import com.bst.framework.datasource.DynamicDataSource;
import com.bst.md.vo.TableFieldVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bst.etl.factory.dbtype.DbTypeMappingFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/base/etl/table")
public class EtlTableOperController extends BaseController {

    @Autowired
    IBaseDbService baseDbService;

    @PostMapping("/query/columns")
    @ResponseBody
    public AjaxResult queryTableColumns(@RequestBody TableColumnsParamVO vo) throws Exception {
        DruidDataSource datasource = (DruidDataSource)DynamicDataSource.getDataSourceMap().get(vo.getIdDb()+"");
        List<TableFieldVO> columns =  DDLUtil.getColumnsFromSqlResult(vo.getQrySql(),datasource);
        return AjaxResult.success(columns);
    }

    @PostMapping("/generateCreateTableSql")
    @ResponseBody
    public AjaxResult generateCreateTableSql(@RequestBody TableColumnsParamVO vo) throws Exception {
        DruidDataSource source = (DruidDataSource)DynamicDataSource.getDataSourceMap().get(vo.getIdDb()+"");
        DruidDataSource target = (DruidDataSource)DynamicDataSource.getDataSourceMap().get("dw");
        String sql = DDLUtil.getCreateTableSql(vo.getQrySql(),vo.getNewTablePrefix()+vo.getTableName(),source,target);
        return AjaxResult.success(sql);
    }

    @PostMapping("/execSql")
    @ResponseBody
    public AjaxResult exec(@RequestBody TableCreatParamVO vo) throws Exception {
        DDLUtil.exec(vo.getSqlCreat());
        return AjaxResult.success("执行成功");
    }

    @PostMapping("/tableForHosAndOdsFlds")
    @ResponseBody
    public AjaxResult tableForHosAndOdsFlds(@RequestBody TableForHosAndOdsParamVO paramVO) {
        List<String> publicFlds = new ArrayList<>();
        try {
            List<TableFieldVO> fldsSource = paramVO.getSourFields();
            DruidDataSource datasource = (DruidDataSource)DynamicDataSource.getDataSourceMap().get("dw");
            List<TableFieldVO> fldsTar =  DDLUtil.getColumnsFromSqlResult("select * from "+paramVO.getTableTar()+" tt where 1=2",datasource);
            List<String> tarFldNames = fldsTar.stream().map(TableFieldVO::getName).collect(Collectors.toList());
            tarFldNames = tarFldNames.stream().map(str->str.toUpperCase()).collect(Collectors.toList());
            for (TableFieldVO fld : fldsSource) {
                if(tarFldNames.contains(fld.getName().toUpperCase())) {
                    publicFlds.add(fld.getName().toUpperCase());
                }
            }
        }catch (Exception e) {}
        return AjaxResult.success(publicFlds);
    }
}
